﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MiUi.ShuaMiHelper
{
    /// <summary>
    /// post 的数据
    /// </summary>
    public class PostData
    {

        /// <summary>
        /// 生成需要POST的数据
        /// </summary>
        /// <returns></returns>
        public string ToString(IList<KeyValue> list)
        {
            return this.ToString(list, Encoding.UTF8);
        }
        /// <summary>
        /// 生成需要POST的数据
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public string ToString(IList<KeyValue> list, Encoding e)
        {
            System.Text.StringBuilder sb = new StringBuilder(list.Count * 10);
            int i = 0;
            foreach (var item in list)
            {

                sb.Append(item.Key + "=" + item.Value + "&");

            }
            return sb.ToString().TrimEnd('&');
        }

        /// <summary>
        /// 根据 字符串 生成 相应的 代码
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public string ToString(string postData)
        {
            string[] str = postData.Split('&');
            StringBuilder sb = new StringBuilder(str.Length * 10);
            sb.Append("IList<KeyValue> list = new List<KeyValue>();" + Environment.NewLine);
            for (int i = 0; i < str.Length; i++)
            {
                string[] s = str[i].Split('=');
                sb.Append(string.Format("list.Add(new KeyValue() {2} Key = {0}{1}{0}, Value = {0}{0} {3});{4}"
                    , "\"", s[0], "{","}", Environment.NewLine));
            }
            return sb.ToString();
        }




    }

    /// <summary>
    /// key,value
    /// </summary>
    public class KeyValue
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }


}
